//
//  PersonalInformationViewController.m
//  HQJFacilitator
//
//  Created by mymac on 16/9/21.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "PersonalInformationViewController.h"
#import "InformationTableViewCell.h"
#import "InformationModel.h"
#import "InformationViewModel.h"
#import "informationNewViewController.h"

@interface PersonalInformationViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *PITableView;
@property (nonatomic,strong) InformationViewModel *viewModel;
@property (nonatomic,strong)NSMutableArray *modelAry;
@property (nonatomic,strong)InformationTableViewCell *myCell;
@property (nonatomic,strong)UITableViewCell *myTwoCell;
@property (nonatomic,strong)InformationModel *inforModel;
@property (nonatomic,copy)NSString *arrStr;
@property (nonatomic,copy)NSString *addessStr;
@end

@implementation PersonalInformationViewController
-(UITableView *)PITableView {
    if (!_PITableView) {
        
        
        
        _PITableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, WIDTH, HEIGHT+20) style:UITableViewStyleGrouped];
//        _PITabbaleView.frame = CGRectMake(0, -20, WIDTH, HEIGHT+20);
        _PITableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _PITableView.delegate = self;
        _PITableView.dataSource = self;
        [_PITableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableView class])];
        [_PITableView registerClass:[InformationTableViewCell class] forCellReuseIdentifier:NSStringFromClass([InformationTableViewCell class])];

        _PITableView.tableFooterView = [UIView new];
        
        _PITableView.sectionFooterHeight = 5;
        _PITableView.sectionHeaderHeight = 5;
      
        
        
    }
    
    
    
    return _PITableView;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _modelAry = [NSMutableArray array];
    _viewModel = [[InformationViewModel alloc]init];
    [_viewModel requst:self.PITableView];
    @weakify(self);
    [_viewModel setMoldeBlock:^(id model) {
        [self_weak_.modelAry addObject:model];
        //        _modelAry = []
    }];
    [_viewModel setArrBlock:^(NSDictionary *str) {
        
        self_weak_.arrStr = [NSString stringWithFormat:@"%@",str[@"arr"]];
        self_weak_.addessStr = [NSString stringWithFormat:@"%@",str[@"address"]];
        HQJLog(@"blcok返回的:%@",str);
        
    }];

    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.PITableView];
   //    self.title = @"个人信息";
//    self.fd_interactivePopDisabled = YES;
    // Do any additional setup after loading the view.
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.arrStr != nil) {
   
    if (indexPath.section == 2 &&indexPath.row ==1)   {
        InformationTableViewCell *cell = self.myCell;
        cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
        CGFloat descriptionH;
           descriptionH  =[self textHeight:self.arrStr];

       
                HQJLog(@"这个高度是%f",descriptionH);

        
        return descriptionH + 20;


    }
    else if (indexPath.section == 0 &&indexPath.row == 4){
        UITableViewCell *cell = self.myTwoCell;
        cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
        CGFloat descriptionH;
        descriptionH  =[self textHeight:self.addessStr];
        
        
        
        return descriptionH+10;

    } else {
        return 44;
 
    }
    } else {
        return 44;

    }

    
}

// 获取 label 实际所需要的高度
- (CGFloat)textHeight:(NSString *)labelText {

    UIFont *tfont = [UIFont systemFontOfSize:14.0];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    //  ios7 的API，判断 labelText 这个字符串需要的高度；    这里的宽度（self.view.frame.size.width - 140he）按照需要自己换就 OK
    CGSize sizeText = [labelText boundingRectWithSize:CGSizeMake(WIDTH - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return sizeText.height+30;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 5;
    } else if (section == 1) {
        
        return 2;
    } else {
        
        return 4;
        
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
   
        InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([InformationTableViewCell class]) forIndexPath:indexPath];
    if (indexPath.section==0) {
        if (indexPath.row == 2 ||indexPath.row == 4) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }
    }
    cell.titleLabel.text = [InformationModel getmodelTitleArySection:indexPath.section][indexPath.row];
    
    if (_modelAry.count != 0) {
        cell.model =_modelAry[0];

    }
    [cell updateLayout];

    return cell;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    informationNewViewController *IFVC = [[informationNewViewController alloc]init];
    if (indexPath.section == 0 && indexPath.row == 2){
        IFVC.title = @"新邮箱";
        [self.navigationController pushViewController:IFVC animated:YES];
    }
    if (indexPath.section == 0 && indexPath.row == 4){
        IFVC.title = @"新地址";
        [self.navigationController pushViewController:IFVC animated:YES];

        
    }
    
    
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
